package com.ycb.bookserver.controller.user;

import com.book.properties.JWTProperties;
import com.book.result.PageResult;
import com.book.result.Result;
import com.ycb.book.dto.BookDTO;
import com.ycb.book.dto.CategoryDTO;
import com.ycb.book.entity.Book;
import com.ycb.book.vo.BookVO;
import com.ycb.book.vo.BookVO1;
import com.ycb.bookserver.service.BookService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ycb.book.dto.BookPageQueryDTO;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired(required = false)
    private JWTProperties jwtProperties;


    @GetMapping("/booklist")
    @ApiOperation("查询所有书籍")
    public Result searchAllBook(){
        log.info("查询所有书籍");
        List<BookVO> bookVOS=bookService.allBook();
        return Result.success(bookVOS);
    }

    @GetMapping("/booklistcategory")
    @ApiOperation("根据分类查询书籍")
    public Result searchByCategory(CategoryDTO categoryDTO){
        log.info("根据分类查询书籍");
        List<BookVO> bookVOS=bookService.BookCategory(categoryDTO);
        return Result.success(bookVOS);
    }


    //新增书籍
    @PostMapping("/addBook")
    @ApiOperation("新增书籍")
    public Result addBook(@RequestBody BookDTO bookDTO){

        log.info("新增书籍:{}",bookDTO);
        System.out.println("当前线程id:"+Thread.currentThread().getId());
        bookService.addBook(bookDTO);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询书籍信息")
    public Result<BookVO1> getById(@PathVariable Long id){
        log.info("查询书籍信息:{}",id);
        BookVO1 book=bookService.getById(id);
        return Result.success(book);
    }
    @PutMapping
    @ApiOperation("修改书籍信息")
    public Result modify(@RequestBody BookDTO bookDTO){
        log.info("修改书籍信息:{}",bookDTO);
        bookService.modify(bookDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("修改书籍交易状态信息")
    public Result sold(@PathVariable Integer status,Long id){
        log.info("商品出售:{},{}",status,id);
        bookService.sold(status,id);
        return Result.success();
    }

    @DeleteMapping("/deleteBook")
    @ApiOperation("删除商品")
    public Result delete(Long id){
        log.info("删除商品:{}",id);
        bookService.delete(id);
        return Result.success();
    }

    @DeleteMapping("/deleteBookS")
    @ApiOperation("批量删除书籍")
    public Result deleteBatch(@RequestParam  List<Long> ids){
        log.info("批量删除书籍:{}",ids);
        bookService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("/searchByName")
    @ApiOperation("搜索书籍")
    public Result<PageResult> search(@RequestBody BookPageQueryDTO bookPageQueryDTO){
        log.info("书籍分页查询:{}",bookPageQueryDTO);
        PageResult pageResult=bookService.search(bookPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/searchByPartName")
    @ApiOperation("根据书籍名字找书")
    public Result searchByPartName(String partName){
        log.info("找书:{}",partName);
        List<BookVO> bookVOS=bookService.searchByPartName(partName);
        return Result.success(bookVOS);
    }
}
